﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class UxDevice : UserControl
    {
        public string DeviceId
        {
            get
            {
                if (ViewState["DeviceId"] != null)
                    return ViewState["DeviceId"].ToString();
                return string.Empty;
            }
            set
            {
                ViewState["DeviceId"] = value;
            }
        }

        public string DeviceCode
        {
            get
            {
                return this.txtDeviceId.Text;
            }
            set { this.txtDeviceId.Text = value; }
        }

        public string DeviceName
        {
            get
            {
                return this.txtDeviceName.Text;
            }
            set { this.txtDeviceName.Text = value; }
        }

        public string Nhanhieu
        {
            get
            {
                return this.txtNhanhieu.Text;
            }
            set { this.txtNhanhieu.Text = value; }
        }

        
        public string Description
        {
            get
            {
                return this.txtGhichu.Text;
            }
            set { this.txtGhichu.Text = value; }
        }

        public bool Status
        {
            get
            {
                return this.chkStatus.Checked;
            }
            set { this.chkStatus.Checked = value; }
        }
        public string StationId
        {
            get
            {
                return this.comboTram.SelectedItem.Value;
            }
            set 
            {
                this.UpdateStation(value);
            }
        }

        private void UpdateStation(string stationId) 
        {
            var item = this.comboTram.Items.FirstOrDefault(x => x.Value.Trim() == stationId.Trim());
            if (item != null)
            {
                var index = this.comboTram.Items.IndexOf(item);
                this.comboTram.SelectedIndex = index;
            }
        }

        public bool InsertOrUpdate
        {
            set
            {
                this.btnUpdate.CommandName = value ? "PerformInsert" : "Update";
                this.btnUpdate.Text = value ? "Insert" : "Update";
            }
            get
            {
                return this.btnUpdate.Text == "Insert";
            }
        }

        public void EnableFieldCode(bool enabled)
        {
            this.txtDeviceId.Enabled = enabled;
        }

        public void SetErrorStatus(string errMessage)
        {
            this.litErrStatus.Text = "<font color='red'>" + errMessage + "</font>";
        }

        public void BindData(VIBA_THIET_BI Device)
        {
            this.DeviceCode = Device.ThietBi_Id;
            this.DeviceName = Device.ThietBi_Ten;
            this.Nhanhieu = Device.ThietBi_NhanHieu;
            this.Description = Device.ThietBi_GhiChu;
            this.StationId =Device.Tram_Id;
            this.Status = Device.Status != 0;
        }

        public VIBA_THIET_BI GetData()
        {
            VIBA_THIET_BI Device = new VIBA_THIET_BI
            {
                ThietBi_Id = this.InsertOrUpdate ? Guid.NewGuid().ToString(): this.DeviceCode,
                ThietBi_Ten = this.DeviceName,
                ThietBi_NhanHieu = this.Nhanhieu,
                Tram_Id = this.StationId,
                ThietBi_GhiChu = this.Description,
                Status=GetStatus(this.Status)
                
            };
            return Device;
        }

        private int GetStatus(bool status)
        {
            return status ? 1 : 0;
        }

        public void AddComboBoxItems()
        {
            using (var db = new BTSModelDataContext())
            {
                var list = db.VIBA_TRAMs;
                this.comboTram.DataSource = list;
                this.comboTram.DataTextField = "Tram_Ten";
                this.comboTram.DataValueField = "Tram_Id";
                this.comboTram.DataBind();
                this.UpdateStation(this.StationId);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
    }
}